var app = getApp()
var upCosUrl = "https://sh.file.myqcloud.com/files/v2/1253272163/alpinter/user_origin_pict"
var stylePath = "http://alpinter-1253272163.cossh.myqcloud.com/style_picture/" 
var stylePicture = new Array("la_muse.ckpt","nianhua0.ckpt","nianhua1.ckpt","nianhua2.ckpt")

Page({
  data: {
    buttontext: '选择一张图片',
    viewPhoto: 'http://alpinter-1253272163.cossh.myqcloud.com/user_origin_pict/4b205e6b06cbfe7ac30f6.jpg',
    viewPhotoName: '4b205e6b06cbfe7ac30f6.jpg',
    stylePhoto: 'http://alpinter-1253272163.cossh.myqcloud.com/style_picture/1-style.jpg',
    styleName:"la_muse.ckpt",
    userInfo: {},
    userImagePath:'',
    userImageName:'',
    bucket: 'alpinter'
  },

  // 回调统一处理函数
  loading: function (isLoading, msg) {
    if (isLoading) {
      wx.showToast({ title: (msg || '正在请求...'), icon: 'loading', duration: 1000 });
    } else {
      wx.hideToast();
    }
  },


  selectPhoto: function () {
    var that = this
    wx.chooseImage({
      count: 1, // 最多可以选择的图片张数，默认9
      sizeType: ['original', 'compressed'], // original 原图，compressed 压缩图，默认二者都有
      sourceType: ['album', 'camera'], // album 从相册选图，camera 使用相机，默认二者都有
      success: function (res) {
        if (res.tempFilePaths && res.tempFilePaths.length) {
          that.loading(1, '正在上传...');
          var filePath = res.tempFilePaths[0];
          that.setData({ viewPhoto: filePath })
          var fileName = filePath.match(/(wxfile:\/\/)(.+)/)
          fileName = that.data.userInfo.nickName + '_' + fileName[2]
          that.setData({ viewPhotoName: fileName })
          console.log(fileName)
          that.setData({userImagePath:filePath,userImageName:fileName})
          that.upload(that.data.viewPhoto, that.data.viewPhotoName)
        }
      }
    });
  },

  upload: function (filepath, fileName) {
    // 鉴权获取签名
    console.log('获取签名')
    wx.request({
      url: 'https://28432247.funulife.com:446/auth',
      data: {
        "tencent_cloud_path": "/user_origin_pict"
      },
      method: 'POST', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
      header: {
        'content-type': 'application/json'
      },
      success: function (cosRes) {
        console.log("auth: " + cosRes.data.data)
        // 头部带上签名，上传文件至COS
        wx.uploadFile({
          url: upCosUrl + '/' + fileName,
          filePath: filepath,
          header: {
            'content-type': 'multipart/form-data',
            'Authorization': cosRes.data.data,
          },
          name: 'filecontent',
          formData: { op: 'upload' },
          success: function (uploadRes) {
            console.log("uploadsuccess " + fileName)
          },
          fail: function (res) {
            console.log(res)
          },
          complete: function(){
            console.log("up load file successful!")
          }
        })
      },
      fail: function(res){
        console.log(res)
      },
      complete: function(){
        console.log("finish get sign")
      }
    })
  },

  changethistype: function(){
    var that = this
    console.log('change' + that.data.styleName)
    wx.request({
      url: 'https://28432247.funulife.com:447/picturechange',
      data: {
        'cloud_path':'/user_origin_pict/' + that.data.viewPhotoName,
        'style_name':that.data.styleName,
        'user_id':that.data.userInfo.nickName
      },
      method: 'POST', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
      header: {
        'content-type': 'application/json'
      }, // 设置请求的 header
      success: function(res){
        console.log(res.data)
        that.setData({ viewPhoto:res.data.data })
        console.log(that.data.viewPhoto)
      },
      fail: function() {
        // fail
      },
      complete: function() {
        // complete
      }
    })
  },

  picturefulldata0: function(){
    var stylePicturePath = stylePath + stylePicture[0] + '.jpg'
    this.setData({
      stylePhoto:stylePicturePath,
      styleName:stylePicture[0]
      })
    this.changethistype()
  },

  picturefulldata1: function(){
    var stylePicturePath = stylePath + stylePicture[1] + '.jpg'
    this.setData({
      stylePhoto:stylePicturePath,
      styleName:stylePicture[1]
      })
    this.changethistype()
  },

  picturefulldata2: function(){
    var stylePicturePath = stylePath + stylePicture[2] + '.jpg'
    this.setData({
      stylePhoto:stylePicturePath,
      styleName:stylePicture[2]
      })
    this.changethistype()
  },
  
  picturefulldata3: function(){
    var stylePicturePath = stylePath + stylePicture[3] + '.jpg'
    this.setData({
      stylePhoto:stylePicturePath,
      styleName:stylePicture[3]
      })
   this.changethistype()
  },

  onLoad: function () {
    console.log('onLoad')
    var that = this
    //调用应用实例的方法获取全局数据
    app.getUserInfo(function (userInfo) {
      //更新数据
      that.setData({
        userInfo: userInfo
      })
    })
  }
})